//(x, y, sightRange, team, me, priorityTargets);
var i, _x=argument0, _y=argument1, _sr=argument2, _its=0, _t=argument3, _me=argument4, _ptargs=argument5, _targetList, _maxdist=10000000, _dist, instance=noone;

_targetList = ds_priority_create();

ds_priority_copy(_targetList, _ptargs);

if (_me.type == VEHICLE)
{
    instance_deactivate_object(_me.collider);
}

while(ds_priority_size(_targetList)>0)
{
    object=ds_priority_delete_max(_targetList);
    for(_its=0;_its<instance_number(object);_its+=1)
    {
        ii = instance_find(object, _its);
        
        if (ii.team != _t && ii != _me)
        {
            _dist=point_distance(_x, _y, ii.x, ii.y);
            if (_dist<=_sr)
            {
                __los = false;
                _me._tx = _x;
                _me._ty = _y;
                _me._ii = ii;
                if (ii.type == VEHICLE)
                {
                    instance_deactivate_object(ii.collider);
                }
                with(_me)
                {
                    if (_ii.type == VEHICLE)
                    {
                        __hit = collision_line(x, y, _ii.x, _ii.y, VisionBlock, false, true);
                        if (!__hit)
                        {
                            instance = _ii;
                        }
                    }
                    else if (!collision_line(_tx, _ty, _ii.x, _ii.y, VisionBlock, false, true))
                    {
                        other.__los = true;
                    }
                }
                if (ii.type == VEHICLE)
                {
                    instance_activate_object(ii.collider);
                }
                if (__los && _dist < _maxdist) 
                {  
                    _maxdist = _dist
                    instance = ii;
                }
            }
        }
    }
}
if (_me.type == VEHICLE)
{
    instance_activate_object(_me.collider);
}
return instance;
